package com.zzk.audiovideodemo

import android.Manifest
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.zzk.audiovideodemo.audiorecorder.AudioRecorderActivity
import com.zzk.audiovideodemo.audiorecorder.AudioRecorderConvertAACActivity
import com.zzk.audiovideodemo.audiorecorder.AudioTrackStaticActivity
import com.zzk.audiovideodemo.audiorecorder.AudioTrackStreamActivity
import com.zzk.audiovideodemo.audiorecorder.convert.MonoToStereoActivity
import com.zzk.audiovideodemo.audiorecorder.convert.StereoToMonoActivity
import com.zzk.audiovideodemo.camera.Camera2TextureViewActivity
import com.zzk.audiovideodemo.camera.CameraXLiteActivity
import com.zzk.audiovideodemo.databinding.ActivityMainBinding
import com.zzk.audiovideodemo.extractorMuxer.ExtractorMuxerUtil
import com.zzk.audiovideodemo.mediacodec.AACToPCMDelegate
import com.zzk.audiovideodemo.mediacodec.MediaCodecVideoPlayerActivity
import com.zzk.audiovideodemo.mediarecorder.*
import com.zzk.audiovideodemo.opengles.GlSurfaceViewActivity
import com.zzk.audiovideodemo.opengles.filter.FilterMainActivity
import com.zzk.audiovideodemo.opengles.particles.ParticleActivity
import com.zzk.audiovideodemo.opengles.texture.GuangZhouTaTextureActivity
import com.zzk.audiovideodemo.surfaceview.video.SurfaceVideoActivity
import com.zzk.audiovideodemo.surfaceview.view.SurfaceViewHandWritingActivity
import com.zzk.audiovideodemo.surfaceview.view.SurfaceViewSinActivity
import com.zzk.audiovideodemo.textureView.TextureViewCamera1ComplexActivity
import com.zzk.audiovideodemo.textureView.TextureViewCamera1Activity
import com.zzk.audiovideodemo.videoview.VideoViewActivity
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.RuntimePermissions

@RuntimePermissions
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var aacToPCMDelegate: AACToPCMDelegate? = null
    private var extractorMuxerUtil: ExtractorMuxerUtil? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogUtils.i("MainActivity->onCreate")
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        aacToPCMDelegate = AACToPCMDelegate(this)
        extractorMuxerUtil = ExtractorMuxerUtil(this)

        // 点击事件
        binding.btSVSinFun.setOnClickListener {
            startActivity(Intent(this, SurfaceViewSinActivity::class.java))
        }
        binding.btSVHandWriting.setOnClickListener {
            startActivity(Intent(this, SurfaceViewHandWritingActivity::class.java))
        }
        binding.btSVVideo.setOnClickListener {
            startActivity(Intent(this, SurfaceVideoActivity::class.java))
        }
        binding.btTextureView.setOnClickListener {
            startActivity(Intent(this, TextureViewCamera1Activity::class.java))
        }
        binding.btTextureView2.setOnClickListener {
            startActivity(Intent(this, TextureViewCamera1ComplexActivity::class.java))
        }
        binding.btMediaRecorder.setOnClickListener {
            startActivity(Intent(this, MediaRecorderAudioActivity::class.java))
        }
        binding.mediaRecorder.setOnClickListener {
            startMediaRecorderWithPermissionCheck()
        }
        binding.btAudioRecord.setOnClickListener {
            startActivity(Intent(this, AudioRecorderActivity::class.java))
        }
        binding.btCameraX.setOnClickListener {
            startActivity(Intent(this, CameraXLiteActivity::class.java))
        }
        binding.convertMonoToStereo.setOnClickListener {
            startConvertMonoToStereoWithPermissionCheck()
        }
        binding.convertStereoToMono.setOnClickListener {
            startConvertStereoToMonoWithPermissionCheck()
        }
        binding.btMediaRecorderBreak.setOnClickListener {
            showMediaRecorderBreakWithPermissionCheck()
        }
        binding.btVideoView.setOnClickListener {
            showVideoViewWithPermissionCheck()
        }
        binding.btMediaCodec.setOnClickListener {
            showMediaCodeCVideoWithPermissionCheck()
        }
        binding.btAudioTrackStatic.setOnClickListener {
            showAudioTrackStaticActivityWithPermissionCheck()
        }
        binding.btAudioTrackStream.setOnClickListener {
            showAudioTrackStreamActivityWithPermissionCheck()
        }
        binding.btAACTOPCM.setOnClickListener {
            aacToPCMDelegate?.aacToPCM()
        }
        binding.btnMP4VideoAndAudio.setOnClickListener {
            extractorMuxerUtil?.extractorAndMuxerMP4()
        }
        binding.btnVideoAndAudioToMP4.setOnClickListener {
            extractorMuxerUtil?.muxerMp4()
        }
        binding.btnGLSurfaceView.setOnClickListener {
            startActivity(Intent(this@MainActivity, GlSurfaceViewActivity::class.java))
        }
        binding.btnGLParticle.setOnClickListener {
            startActivity(Intent(this@MainActivity, ParticleActivity::class.java))
        }
        binding.btnGLTexture.setOnClickListener {
            startActivity(Intent(this@MainActivity, GuangZhouTaTextureActivity::class.java))
        }
        binding.btnGLFilter.setOnClickListener {
            startActivity(Intent(this@MainActivity, FilterMainActivity::class.java))
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResult(requestCode, grantResults)
    }

    private fun jumpToActivity(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        try {
            startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            Toast.makeText(this, "Target screen not found.", Toast.LENGTH_SHORT).show()
        }
    }

    @NeedsPermission(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun startMediaRecorder() {
        startActivity(Intent(this@MainActivity, MediaRecorderVideoActivity::class.java))
    }

    @NeedsPermission(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun startConvertMonoToStereo() {
        startActivity(Intent(this@MainActivity, MonoToStereoActivity::class.java))
    }

    @NeedsPermission(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun startConvertStereoToMono() {
        startActivity(Intent(this@MainActivity, StereoToMonoActivity::class.java))
    }

    fun showCamera2TextureView(view: View?) {
        jumpToActivity(Camera2TextureViewActivity::class.java)
    }

    @NeedsPermission(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun showAudioRecorderConvertAAC(view: View?) {
        jumpToActivity(AudioRecorderConvertAACActivity::class.java)
    }

    @NeedsPermission(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun showMediaRecorderBreak() {
        startActivity(Intent(this@MainActivity, MediaRecorderVideoBreakActivity::class.java))
    }

    @NeedsPermission(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
    )
    fun showVideoView() {
        startActivity(Intent(this@MainActivity, VideoViewActivity::class.java))
    }

    @NeedsPermission(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )
    fun showMediaCodeCVideo() {
        startActivity(Intent(this@MainActivity, MediaCodecVideoPlayerActivity::class.java))
    }

    @NeedsPermission(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
    )
    fun showAudioTrackStreamActivity() {
        startActivity(Intent(this@MainActivity, AudioTrackStreamActivity::class.java))
    }

    @NeedsPermission(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
    )
    fun showAudioTrackStaticActivity() {
        startActivity(Intent(this@MainActivity, AudioTrackStaticActivity::class.java))
    }
}